package com.alertdroid2;

import java.io.IOException;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;


import android.content.Context;
import android.database.SQLException;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AlertAlarm extends MapActivity {
	private static final String LOG_TAG = "MyLocation";
	Uri alert;
	MediaPlayer mMediaPlayer;
	TextView namaMemo;
	TextView placeMemo;
	TextView timeMemo;
	MapView mapView;
	Button btnStop;
	MapController mapController;
	DatabaseHelper myDbHelper;
	Location location;
	LocationManager locationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alertviewmemo);
		

		// alert uri
		alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
		if (alert == null) {
			// alert is null, using backup
			alert = RingtoneManager
					.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
			if (alert == null) {
				alert = RingtoneManager
						.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
			}
		}

		// play ringtone
		try {
			playRingtone();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			initDb();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		final int idMemo = getIntent().getIntExtra(alertdroidConstants.IDMEMO,
				-1);

		if (idMemo != -1) {
			MemoClass memoClass = myDbHelper.getMemoById(idMemo);

			namaMemo = (TextView) findViewById(R.id.nameMemo);
			placeMemo = (TextView) findViewById(R.id.placeMemo);
			timeMemo = (TextView) findViewById(R.id.timeMemo);
			mapView = (MapView) findViewById(R.id.mapview1);

			namaMemo.setText(memoClass.nama);
			placeMemo.setText(memoClass.place);
			timeMemo.setText(memoClass.waktu.format("%d-%m-%Y %H:%M"));

			int longitude = memoClass.longitude;
			int latitude = memoClass.latitude;

			mapView.setBuiltInZoomControls(true);
			mapView.setStreetView(true);
			mapController = mapView.getController();
			mapController.setZoom(14); // Zoon 1 is world view

			// point to memo location
			GeoPoint point = new GeoPoint(latitude, longitude);
			mapController.animateTo(point);

			// draw marker
			mapView.getOverlays().add(new MyOverLay(point, "event"));
		} else
			try {
				throw new Exception("data -1");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		
		// button stop
		btnStop = (Button) findViewById(R.id.btnStop);
		btnStop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		// get my location
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 60000, 0,
				new GeoUpdateHandler());
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				60000, 0, new GeoUpdateHandler());
	}

	public void playRingtone() throws IllegalStateException, IOException {
		mMediaPlayer = new MediaPlayer();
		mMediaPlayer.setDataSource(this, alert);
		final AudioManager audioManager = (AudioManager) this
				.getSystemService(AlertAlarm.AUDIO_SERVICE);
		if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
			mMediaPlayer.setLooping(true);
			mMediaPlayer.prepare();
			mMediaPlayer.start();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		myDbHelper.closeDb();
		super.onDestroy();
	}

	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	private void initDb() throws IOException {
		myDbHelper = new DatabaseHelper(this);
		myDbHelper.createDataBase();
		try {
			myDbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
	}

	public class GeoUpdateHandler implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);
			Log.i(LOG_TAG, "Location : " + location.getLatitude() + ","
					+ location.getLongitude());
			GeoPoint point = new GeoPoint(lat, lng);
			// mapController.animateTo(point);

			// draw marker
			//mapView.getOverlays().add(new MyOverLay(point, "mylocation"));
		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}

	}

}
